package com.hx0311.pas.security.usertype.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hx0311.core.dao.BaseDao;
import com.hx0311.core.dao.support.ResultSetUtil;
import com.hx0311.pas.security.usertype.model.UserType;

public class UserTypeDao extends BaseDao{

	private Connection conn;
	private PreparedStatement stat;

	public void add(UserType type) {
		if (type != null) {
			String sql = "insert into rylbb (rylb, lbmc) values (?, ?)";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setString(1, type.getRylb());
				stat.setString(2, type.getLbmc());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				close(stat, conn);
			}
		}
	}

	public void update(UserType type) {
		if (type != null) {
			String sql = "update rylbb set lbmc = ? where rylb = ?";
			try {
				conn = getConnection();
				stat = conn.prepareStatement(sql);
				stat.setString(1, type.getLbmc());
				stat.setString(2, type.getRylb());
				stat.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}finally{
				close(stat, conn);
			}
		}
	}

	@SuppressWarnings("unchecked")
	public List getAll() {
		String sql = "select rylb, lbmc from rylbb";
		ResultSet rs = null;
		try {
			conn = getConnection();
			stat = conn.prepareStatement(sql);
			rs = stat.executeQuery();
			return ResultSetUtil.toList(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			close(rs, stat, conn);
		}
		return java.util.Collections.EMPTY_LIST;
	}
	
	@Override
	public String getIdName() {
		return "rylb";
	}

	@Override
	public String getTableName() {
		return "rylbb";
	}
	
}
